from app.outdoor.models import Activity
from app.account.models import UserInfo, UserSetting
from rest_framework.views import APIView
from django.http import JsonResponse
from common.authCheck.userAuth import check_login, getUser
from common.dictInfo import model_to_dict


class UserActivityInfoView(APIView):

    @check_login
    def get(self, request, uid=0):
        """
        获取用户参加活动的列表
        :param request:
        :param uid:
        :return:
        """
        if uid != 0:
            """查看他人信息"""
            user = UserInfo.objects.filter(id=uid)
            if not user.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '用户不存在'
                }, status=404)
            user = user[0]
            if UserSetting.objects.get(related_user=user).activity_privacy == '2':
                return JsonResponse({
                    'status': True,
                    'user_activity': '用户设置了隐私，未能查看'
                })
        else:
            user = getUser(request.session.get('login'))
        activity = Activity.objects.filter(participants=user)
        activityDict = [model_to_dict(ac) for ac in activity]
        return JsonResponse({
            'status': True,
            'activity': activityDict
        })
